package com.ziq.zcamp.place

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.facebook.imagepipeline.request.ImageRequest
import com.qweather.sdk.bean.weather.WeatherDailyBean
import com.ziq.base.extension.click
import com.ziq.base.extension.dp
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.bean.place.PlaceDetail
import com.ziq.zcamp.bean.place.PlaceCondition
import com.ziq.zcamp.databinding.ItemPlaceDetailParametersBinding
import com.ziq.zcamp.dialog.WeatherDialog
import com.ziq.zcamp.util.ImageUtil
import com.ziq.zcamp.util.WeatherUtil
import com.ziq.zcamp.view.SafetySimpleDraweeView

class PlaceParametersDetailGroup(val activity: PlaceDetailActivity, val adapter: PlaceDetailAdapter) : DetailGroup() {

    override fun getHeaderViewType(groupPosition: Int): Int {
        return 0
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return PlaceDetailAdapter.TYPE_CHILD_PARAMETERS
    }

    override fun getFooterViewType(groupPosition: Int): Int {
        return 0
    }

    var placeDetail: PlaceDetail? = null

    var weatherDailyBean : WeatherDailyBean.DailyBean? = null
    var campParametersList = LinkedHashMap<String, MutableList<PlaceCondition>>()

    var showAllCampParameters = false

    //详情
    fun setData(placeDetail: PlaceDetail?) {
        this.placeDetail = placeDetail
        if(placeDetail?.camp?.condition_list?.isNotEmpty() == true){
            campParametersList.clear()
            for (item in placeDetail.camp?.condition_list!!){
                val categoryName = item.parent_name
                if(categoryName?.isNotEmpty() == true){
                    if(campParametersList.containsKey(categoryName)){
                        campParametersList[categoryName]?.add(item)
                    } else {
                        campParametersList[categoryName] = mutableListOf(item)
                    }
                }
            }
        }
    }

    fun setWeatherData(weatherDailyBean : WeatherDailyBean.DailyBean?){
        this.weatherDailyBean = weatherDailyBean
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return false
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return false
    }

    override fun getChildrenCount(): Int {
        return 1
    }

    override fun onBindHeaderViewHolder( holder: BaseViewHolder?, groupPosition: Int) {

    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {

        val layoutParams = holder?.itemView?.layoutParams
        if (layoutParams != null && layoutParams is StaggeredGridLayoutManager.LayoutParams) {
            layoutParams.isFullSpan = true
        }
        holder?.let {
            val binding = ItemPlaceDetailParametersBinding.bind(holder.itemView)

            val lp = binding.container.layoutParams as StaggeredGridLayoutManager.LayoutParams
            if(groupPosition == 0){
                lp.topMargin = 0
            } else {
                lp.topMargin = 8.dp
            }
            binding.container.layoutParams = lp

            binding.llCampParametersAction.click {
                showAllCampParameters = !showAllCampParameters
                if(showAllCampParameters){
                }
                updateCampParametersListUI(binding)
            }

            updateCampParametersListUI(binding)


            weatherDailyBean?.let {
                binding.tvWeather.text = it.textDay
                binding.ivWeather.setImageResource(WeatherUtil.getWeatherResByCode(it.iconDay))
                binding.tvTemperatureHigh.text = "${it.tempMax}°C"
                binding.tvTemperatureLow.text = "${it.tempMin}°C"
            }
            binding.btnLookWeather.click {
                if(activity != null){
                    WeatherDialog(activity, placeDetail?.camp?.longitude?:"", placeDetail?.camp?.latitude?:"").show()
                }
            }

        }
    }

    fun updateCampParametersListUI(binding : ItemPlaceDetailParametersBinding){
        if(campParametersList.isNotEmpty()){
            binding.clCampParameters.visibility = View.VISIBLE
            if(showAllCampParameters){
                binding.tvCampParametersAction.text = "收起"
                binding.ivCampParametersAction.setImageResource(R.drawable.icon_parameter_close)
            } else {
                binding.tvCampParametersAction.text = "展开更多"
                binding.ivCampParametersAction.setImageResource(R.drawable.icon_parameter_open)
            }
            binding.llCampParametersList.removeAllViews()
            var hasBreak = false
            var rowNum = 0
            var rowNumLimit = 4 //多于 5行显示更多按钮
            for (name in campParametersList.keys){
                if(!showAllCampParameters && rowNum == rowNumLimit){
                    hasBreak = true
                    break
                }
                val rowView: LinearLayout =
                    LayoutInflater.from(binding.llCampParametersList.context).inflate(R.layout.layout_camp_parameter_category_row, null, false) as LinearLayout
                rowView.findViewById<TextView>(R.id.tv_category_name).text = " ·${name}"
                val rowViewContent = rowView.findViewById<LinearLayout>(R.id.ll_category_content)

                if(campParametersList[name] != null){
                    val length = campParametersList[name]!!.size
                    var row: ViewGroup? = null
                    for(index in 0 until length){
                        val item = campParametersList[name]!!.get(index)
                        val rowIndex = index % 3
                        if(rowIndex == 0){
                            if(!showAllCampParameters && rowNum == rowNumLimit){
                                hasBreak = true
                                break
                            }
                            row = LayoutInflater.from(binding.llCampParametersList.context).inflate(R.layout.layout_camp_parameter_category_item_row, null, false) as ViewGroup?
                            rowViewContent.addView(row)
                            rowNum++
                        }
                        val rowItem = LayoutInflater.from(binding.llCampParametersList.context).inflate(R.layout.item_camp_parameter, null, false)
                        ImageUtil.updateImageFromNetwork(rowItem.findViewById<SafetySimpleDraweeView>(R.id.image), item.icon?:"", ImageRequest.CacheChoice.DEFAULT)
                        rowItem.findViewById<TextView>(R.id.text).text = item.name
                        rowItem.findViewById<TextView>(R.id.text).setTextColor(if (item.value == 1) Color.parseColor("#6E5F55") else Color.parseColor("#C5BFBB"))
                        (row?.getChildAt(rowIndex) as ViewGroup?)?.addView(rowItem)
                    }
                }
                binding.llCampParametersList.addView(rowView)
            }
            binding.llCampParametersAction.visibility = if(hasBreak || rowNum > rowNumLimit) View.VISIBLE else View.GONE
        } else {
            binding.clCampParameters.visibility = View.GONE
        }

        val lp = binding.clTravelInfo.layoutParams as LinearLayout.LayoutParams
        if(binding.clCampParameters.visibility == View.GONE){
            lp.topMargin = 0
        } else {
            lp.topMargin = 8.dp
        }
        binding.clTravelInfo.layoutParams = lp
    }

    override fun onBindFooterViewHolder(holder: BaseViewHolder?, groupPosition: Int) {
    }

}